# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from django.db import models
from django.core.urlresolvers import reverse
from tinymce import models as tinymce_models


class Category(models.Model):
    title = models.CharField(max_length=150, verbose_name=u"Заголовок категории")
    slug = models.SlugField(unique=True, verbose_name=u"отображаемая ссылка")
    operator = models.ManyToManyField(User, blank=True, null=True, verbose_name=u"оператор категории")

    class Meta:
        verbose_name=u'Категория'
        verbose_name_plural = u'Категории'

    def __unicode__(self):
        return u'%s' % self.title

class News(models.Model):
    title = models.CharField(max_length=150, verbose_name=u"Заголовок новости")
    category = models.ForeignKey(Category, verbose_name=u"Категория новости")
    body = tinymce_models.HTMLField(verbose_name=u"Тело новости")
    date = models.DateTimeField(auto_now=True, verbose_name=u"Время создания новости")
    enabled = models.BooleanField(default=True, verbose_name=u"Активна ли новость?")

    class Meta:
        verbose_name=u'Новость'
        verbose_name_plural = u'Новости'

    def __unicode__(self):
        return u'%s' % self.title

    def get_absolute_url(self):
        return reverse('news_detail', args=[self.category.slug, self.pk])

    def category_link(self):
        url = reverse("admin:news_category_change", args=[self.category.pk])
        return u'<a href="%s" title="Посмотреть данные категории" class="order_link"><strong>%s</strong></a>' %\
               (url, self.category.title)
    category_link.allow_tags = True
    category_link.short_description = u"Категория"